define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'myvultrinstances/index' + location.search,
                    add_url: 'myvultrinstances/add',
                    edit_url: 'myvultrinstances/edit',
                    del_url: 'myvultrinstances/del',
                    multi_url: 'myvultrinstances/multi',
                    import_url: 'myvultrinstances/import',
                    table: 'myvultr_instances',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'instances_id', title: __('Instances_id'), operate: 'LIKE'},
                        {field: 'os_id', title: __('Os_id'), operate: 'LIKE'},
                        {field: 'plan', title: __('Plan'), operate: 'LIKE'},
                        {field: 'region', title: __('Region'), operate: 'LIKE'},
                        {field: 'power_status', title: __('Power_status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'server_status', title: __('Server_status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'status', title: __('实例状态'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#c-continent').on('change',function () {
                $("#c-region").selectPageClear();
            });
            $("#c-region").data("params", function(){
                return {custom: {continent: $("#c-continent").val()}};
            });

            $('#c-ostype').on('change',function () {
                $("#c-os_id").selectPageClear();
            });
            $("#c-os_id").data("params", function(){
                return {custom: {family: $("#c-ostype").val()}};
            });

            $('#c-region').on('change',function () {
                $("#c-plan").selectPageClear();
            });
            $("#c-region").data("formatItem",function (item) {
                $("#c-region_id").val(item.regions_id);
                return "国家:"+item.country+",地区:"+item.city_cn;
            });
            $("#c-plan").data("formatItem",function (item) {
                return item.disk+"GB固态硬盘，"+item.monthly_cost+"美元/月";
            });
            $("#c-plan").data("params", function(){
                return {custom: {regions_id: $("#c-region_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});